package com.lqh.flutterModuleFirst

import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.android.FlutterView
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.embedding.engine.FlutterEngineCache
import io.flutter.embedding.engine.dart.DartExecutor
import io.flutter.plugin.common.EventChannel
import io.flutter.plugin.common.MethodChannel
import java.util.*

//class FlutterModuleActivity : AppCompatActivity() {
//    override fun onCreate(savedInstanceState: Bundle?) {
//        super.onCreate(savedInstanceState)
//        setContentView(R.layout.activity_flutter_module_actvity)
//        // 启动 Flutter 引擎
//        val flutterEngine = FlutterEngine(this)
//        flutterEngine.dartExecutor.executeDartEntrypoint(
//            DartExecutor.DartEntrypoint.createDefault()
//        )
//        FlutterEngineCache.getInstance().put("my_engine_id", flutterEngine)
//
//        // 设置 FlutterView
//        val flutterView = findViewById<FlutterView>(R.id.flutter_view)
//        flutterView.attachedFlutterEngine(flutterEngine)
//    }
//}

//
//class FlutterModuleActivity : AppCompatActivity() {
//    private lateinit var flutterEngine: FlutterEngine
//
//    override fun onCreate(savedInstanceState: Bundle?) {
//        super.onCreate(savedInstanceState)
//        setContentView(R.layout.activity_flutter_module_actvity)
//
//        // 启动 Flutter 引擎
//        flutterEngine = FlutterEngine(this)
//        flutterEngine.dartExecutor.executeDartEntrypoint(
//            DartExecutor.DartEntrypoint.createDefault()
//        )
//        FlutterEngineCache.getInstance().put("my_engine_id", flutterEngine)
//
//        // 设置 FlutterView - 使用 FlutterActivity 的方式
//        val flutterView = findViewById<FlutterView>(R.id.flutter_view)
//        // 直接设置引擎
//        flutterView.attachToFlutterEngine(flutterEngine)
//
//        // 或者使用这种方式
////         flutterView.setFlutterEngine(flutterEngine)
//    }
//
//    override fun onDestroy() {
//        super.onDestroy()
//        flutterEngine.destroy()
//    }
//}


class FlutterModuleActivity : AppCompatActivity() {
    private lateinit var flutterEngine: FlutterEngine
    private lateinit var communicationHelper: FlutterCommunicationHelper

    companion object {
        private const val TAG = "FlutterModuleActivity"
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_flutter_module_actvity)

        // 启动 Flutter 引擎
        flutterEngine = FlutterEngine(this)

        // 开发环境使用release版本的Flutter模块
        // 不再连接到开发服务器，直接使用编译后的代码
        flutterEngine.dartExecutor.executeDartEntrypoint(
            DartExecutor.DartEntrypoint.createDefault()
        )

        // 设置通讯 - 在引擎初始化后立即设置
        communicationHelper = FlutterCommunicationHelper(this, flutterEngine)
        communicationHelper.setupChannels()

        // 缓存引擎
        FlutterEngineCache.getInstance().put("my_engine_id", flutterEngine)

        // 设置 FlutterView
        val flutterView = findViewById<FlutterView>(R.id.flutter_view)
        flutterView.attachToFlutterEngine(flutterEngine)

        Log.d(TAG, "FlutterModuleActivity initialized with communication channels")
    }

    override fun onDestroy() {
        super.onDestroy()
        flutterEngine.destroy()
        Log.d(TAG, "FlutterModuleActivity destroyed")
    }
}



//
//class FlutterModuleActivity : AppCompatActivity() {
//    override fun onCreate(savedInstanceState: Bundle?) {
//        super.onCreate(savedInstanceState)
//        setContentView(R.layout.activity_flutter_module_actvity)
//        // 启动时传递路由和参数
////        startActivity(
////            FlutterActivity
////                .withNewEngine()
////                .initialRoute("/detail?item_id=123")
////                .build(this)
////        )
////        startActivity(FlutterActivity.createDefaultIntent(this))
//    }
//}